/*******************************************************************************
 * Copyright (c) 2012 Alexandr Tsvetkov.
 * All rights reserved. This program and the accompanying materials
 * are made available under the terms of the GNU Public License v2.0
 * which accompanies this distribution, and is available at
 * http://www.gnu.org/licenses/old-licenses/gpl-2.0.html
 *
 * Contributors:
 *     Alexandr Tsvetkov - initial API and implementation
 *
 * Project:
 *     TAO Library
 *
 * File name:
 *     Rout.java
 *
 * License agreement:
 *
 * 1. This code is published AS IS. Author is not responsible for any damage that can be
 *    caused by any application that uses this code.
 * 2. Author does not give a garantee, that this code is error free.
 * 3. This code can be used in NON-COMMERCIAL applications AS IS without any special
 *    permission from author.
 * 4. This code can be modified without any special permission from author IF AND ONLY IF
 *    this license agreement will remain unchanged.
 * 5. SPECIAL PERMISSION for this code usage in COMMERCIAL application SHOULD be obtained
 *    from author.
 ******************************************************************************/
package ua.at.tsvetkov.ui;

import ua.at.tsvetkov.util.Log;
import android.app.Activity;
import android.app.AlertDialog;
import android.app.ProgressDialog;
import android.app.AlertDialog.Builder;
import android.widget.Toast;

/**
 * Showing and managing for toasts, dialogs, progress inside running thread in UI thread.
 *
 * @author A.Tsvetkov 2011, http://tsvetkov.at.ua, al@ukr.net
 */
public class Rout {

	private static CharSequence	ok = "Ok";

	/**
	 * Show toast in UI thread.
	 *
	 * @param activity Activity
	 * @param resId id string resource
	 * @param duration (<code>Toast.LENGTH_SHORT</code> или <code>Toast.LENGTH_LONG</code>)
	 */
	public static void toast(final Activity activity, final int resId, final int duration) {
		activity.runOnUiThread(new Runnable() {

			@Override
			public void run() {
				Toast.makeText(activity, resId, duration).show();
			}
		});

	}

	/**
	 * Show toast in UI thread.
	 *
	 * @param activity  Activity
	 * @param text message
	 * @param duration (<code>Toast.LENGTH_SHORT</code> или <code>Toast.LENGTH_LONG</code>)
	 */
	public static void toast(final Activity activity, final String text, final int duration) {
		activity.runOnUiThread(new Runnable() {

			@Override
			public void run() {
				Toast.makeText(activity, text, duration).show();
			}
		});

	}

	/**
	 * Show progress dialog in UI thread.
	 *
	 * @param context 
	 * @param dialogId 
	 */
	public static void showDialog(final Activity context, final int dialogId) {
		context.runOnUiThread(new Runnable() {

			@SuppressWarnings("deprecation")
			@Override
			public void run() {
				context.showDialog(dialogId);
			}
		});
	}

	/**
	 * Remove progress dialog in UI thread.
	 *
	 * @param context 
	 * @param dialogId 
	 */
	public static void removeDialog(final Activity context, final int dialogId) {

		context.runOnUiThread(new Runnable() {

			@SuppressWarnings("deprecation")
			@Override
			public void run() {
				context.removeDialog(dialogId);
			}
		});
	}

	/**
	 * Dismiss progress dialog in UI thread.
	 *
	 * @param context 
	 * @param dialogId 
	 */
	public static void dismissDialog(final Activity context, final int dialogId) {
		context.runOnUiThread(new Runnable() {

			@SuppressWarnings("deprecation")
			@Override
			public void run() {
				try {
					context.dismissDialog(dialogId);
				} catch (IllegalArgumentException e) {
					Log.w("Dialog with id" + dialogId + " was not previously shown via showDialog(int).", e);
				}
			}
		});
	}

	/**
	 * Progress setup.
	 *
	 * @param context 
	 * @param progress 
	 * @param value 
	 */
	public static void setProgress(final Activity context, final ProgressDialog progress, final int value) {
		context.runOnUiThread(new Runnable() {

			@Override
			public void run() {
				progress.setProgress(value);
			}
		});
	}

	/**
	 * Increment progress
	 *
	 * @param context 
	 * @param progress 
	 * @param value
	 */
	public static void incrementProgressBy(final Activity context, final ProgressDialog progress, final int value) {
		if (progress != null) {
			context.runOnUiThread(new Runnable() {

				@Override
				public void run() {
					progress.incrementProgressBy(value);
				}
			});
		}
	}

	/**
	 * Show Alert on UI Thread.
	 *
	 * @param alert
	 */
	private static void draw(Activity activity, final Builder alert) {
		// TODO Need to better check
		if (!activity.hasWindowFocus()) {
			Log.w(activity.getClass().getSimpleName() + " is haven't focus. Can't show alert.");
			return;
		}
		activity.runOnUiThread(new Runnable() {

			@Override
			public void run() {
				alert.create().show();
			}
		});
	}

	/**
	 * Show Alert on UI Thread. 
	 * @param title
	 * @param msg
	 */
	public static void showAlert(Activity activity, int title, int msg) {
		Builder alert = new AlertDialog.Builder(activity);
		alert.setPositiveButton(getOk(), null);
		alert.setTitle(title);
		alert.setMessage(msg);
		draw(activity, alert);
	}

	/**
	 * Show Alert on UI Thread. 
	 *
	 * @param title
	 * @param msg
	 */
	public static void showAlert(Activity activity, int title, CharSequence msg) {
		Builder alert = new AlertDialog.Builder(activity);
		alert.setPositiveButton(getOk(), null);
		alert.setTitle(title);
		alert.setMessage(msg);
		draw(activity, alert);
	}

	/**
	 * Show Alert on UI Thread. 
	 *
	 * @param title
	 * @param msg
	 */
	public static void showAlert(Activity activity, CharSequence title, int msg) {
		Builder alert = new AlertDialog.Builder(activity);
		alert.setPositiveButton(getOk(), null);
		alert.setTitle(title);
		alert.setMessage(msg);
		draw(activity, alert);
	}

	/**
	 * Show Alert on UI Thread. 
	 *
	 * @param title
	 * @param msg
	 */
	public static void showAlert(Activity activity, CharSequence title, CharSequence msg) {
		Builder alert = new AlertDialog.Builder(activity);
		alert.setPositiveButton(getOk(), null);
		alert.setTitle(title);
		alert.setMessage(msg);
		draw(activity, alert);
	}

	public static CharSequence getOk() {
		return ok;
	}

	public static void setOk(String stOk) {
		ok = stOk;
	}

}
